package com.googlecode.phpnodb;

import java.io.File;

import org.eclipse.epsilon.common.util.StringProperties;
import org.eclipse.epsilon.egl.EglFileGeneratingTemplate;
import org.eclipse.epsilon.egl.EglFileGeneratingTemplateFactory;
import org.eclipse.epsilon.egl.EglTemplateFactoryModuleAdapter;
import org.eclipse.epsilon.egl.EgxModule;
import org.eclipse.epsilon.egl.internal.EglModule;
import org.eclipse.epsilon.egl.internal.IEglModule;
import org.eclipse.epsilon.egl.util.FileUtil;
import org.eclipse.epsilon.emc.google.spreadsheet.GSModel;
import org.eclipse.epsilon.emc.google.spreadsheet.SpreadsheetModel;
import org.eclipse.epsilon.eol.IEolExecutableModule;
import org.eclipse.epsilon.eol.IEolModule;
import org.eclipse.epsilon.eol.execute.context.Variable;

import com.beust.jcommander.JCommander;

public class PhpnodbGenerator {
	
	public static void main(String[] args) throws Exception {
		PhpnodbGeneratorParameters parameters = new PhpnodbGeneratorParameters();
		new JCommander(parameters, args);
		PhpnodbGenerator pg = new PhpnodbGenerator();
		pg.generate(parameters.spreadsheet, parameters.username, parameters.password);
	}
	
	protected File outputDirectory = null;
		
	protected void generate(String spreadsheet, String username, String password) throws Exception {
		
		EglFileGeneratingTemplateFactory templateFactory = new EglFileGeneratingTemplateFactory();
		//templateFactory.setOutputRoot(directory.getAbsolutePath());
		EglTemplateFactoryModuleAdapter module = new EglTemplateFactoryModuleAdapter(templateFactory);
		
		module.parse(PhpnodbGenerator.class.getResource("phpnodb.egl").toURI());
		//module.getContext().getModelRepository().addModel(model);
		
		GSModel model = new GSModel();
		model.username = username;
		model.password = password;
		model.spreadsheetName = spreadsheet;
		model.load();
		
		module.getContext().getFrameStack().putGlobal(Variable.createReadOnlyVariable("M", model));
		FileUtil.write(new File(spreadsheet + ".php").getAbsolutePath(), module.execute()+"", false);
		
		
	}
	
}
